(function($) {
    /**
     * 高亮显示当前链接元素[现在还得基于Jquery]
     * 
     * <code>
     * //HTML部分:
     * <div class="navmenu">
         * <ul>
             * <li><a href="link1">link1</a></li>
             * <li><a href="link2">link2</a></li>
             * <li><a href="link3">link3</a></li>
         * </ul>
     * </div>
     * //JS部分：
     *  $.highLightElement('.navmenu ul li', 'active', -1);
     * </code>
     *
     * @param string findDom 查找的当前元素
     * @param string currentClass 当前选中的样式名
     * @param string level 相对于当前查到元素的层级关系，往下是-N, 往上是+N
     * @param string callback 对找到的目标附加的函数调用
     *
     * @return void
     */
    $.fn.highLightElement    = function(findDom, currentClass, level, callback) {
        var modeFileExp = '';
        var fileType    = '';
        var modeFlag    = 0;
        var sameMode    = {};   //{thread: "group"};
        var otherMode   = {};   //{my: "ac", tw: "ac", apps: "app", faq: "ct"};
        var otherParam  = null;
        var sameOtherParam  = {};   //{my: {match: "ac=picture", param: "my.php?ac=profile"}};
        var url         = window.location.href;
        var nowHref     = url.substring(url.lastIndexOf('/') + 1);//拿到当前访问的页面及查询内容
        if(typeof nowHref == 'undefined' || nowHref == '') {
            return true;
        }
        if(nowHref.indexOf('?') > 0) {
            var nowPage     = nowHref.substring(0, nowHref.indexOf('?')); //拿到当前的访问页面，不要.php
        } else {
            var nowPage     = nowHref;
        }
        for(element in sameMode) {
            if(nowPage == element) {
                nowPage     = sameMode[element];
                break;
            }
        }
        //拿到其它的模式
        for(var item in otherMode) {
            if(nowPage == item) {
                modeFlag       = 1;
                var oMode  = new RegExp(otherMode[item] + "=(\\w+)" , "gi");
                otherParam     = nowHref.match(oMode);  //拿到其它的决定变量
                if(otherParam) {
                    otherParam  = otherParam[0];
                    var paramFlag   = 0;
                    for(var page in sameOtherParam) {
                        if(nowPage == page && otherParam == sameOtherParam[page].match){
                            paramFlag   = 1;
                            otherParam  = null;
                            nowHref     = sameOtherParam[page].param;
                        }
                    }
                    if(paramFlag == 0) {
                        //otherParam  = new RegExp(otherParam, 'gi');
                    }
                }
                break;
            }
        }
        var maskLoc     =  nowHref.indexOf('?');
        if(maskLoc != -1) {
            modeFileExp    = nowHref.substring(nowHref.indexOf('.'), maskLoc);
        } else {
            modeFileExp        = nowPage;
        }

        if(typeof level == 'undefined') {
            level = 0;
        }
        jQuery(findDom).each(function() {
            var curHref     = jQuery(this).children('a').attr('href');
            if(typeof curHref != 'undefined' && curHref != '') {
                if(curHref.match(modeFileExp) != null) {
                    if(modeFlag == 1) {
                        if(otherParam == null && curHref != nowHref) {
                            return true;

                        } else if(otherParam != null && curHref.match(otherParam) == null) {
                            return true;
                        }
                    }
                    var targetDom = $(this);
                    if(level < 0) {
                        for(var i = 0; i > level; i --) {
                            targetDom    = jQuery(targetDom).children('a'); 
                        }
                    } else if(level > 0) {
                        for(var i = 0; i < level; i ++) {
                            targetDom    = jQuery(targetDom).parent(); 
                        }
                    }
                    if(typeof callback != 'undefined') {
                        callback(targetDom[0]);
                    }
                    jQuery(targetDom[0]).addClass(currentClass);
                    
                    return false;
                }
            }
        });
    };
})(jQuery);